export const state = () => ({
})

export const actions = {
    getItems({}, username) {
        return this.$api.cart.getItems(username).then((res) => {
            console.log(res)
            if(res.data.status === 'OK') {
                console.log(res.data)
                return Promise.resolve(res.data.data)
            }
            else {
                return Promise.reject(res.data.msg)
            }
        }, (error) => {
            return Promise.reject(error)
        }).catch((error) => {
            return Promise.reject(error)
        })
    },
    deleteItem({}, {username, itemId}) {
        return this.$api.cart.deleteItem(username, itemId).then((res) => {
            if(res.data.status === 'OK') {
                return Promise.resolve('OK')
            }
            else {
                return Promise.reject(new Error(res.data.msg))
            }
        }, (error) => {
            return Promise.reject(error)
        }).catch((error) => {
            return Promise.reject(error)
        })
    },
    addItem({}, {username, itemId}) {
        return this.$api.cart.addItem(username, itemId).then((res) => {
            if(res.data.status === 'OK') {
                return Promise.resolve('OK')
            }
            else {
                return Promise.reject(new Error(res.data.msg))
            }
        }, (error) => {
            return Promise.reject(error)
        }).catch((error) => {
            return Promise.reject(error)
        })
    },
    updateItem({}, {username, itemId, type}) {
        return this.$api.cart.updateItem(username, itemId, type).then((res) => {
            if(res.data.status === 'OK') {
                return Promise.resolve('OK')
            }
            else {
                return Promise.reject(res.data.msg)
            }
        }, (error) => {
            return Promise.reject(error)
        }).catch((error) => {
            return Promise.reject(error)
        })
    },
    clearCart({}, username) {
        return this.$api.cart.clearCart(username).then((res) => {
            if(res.data.status === 'OK') {
                return Promise.resolve('OK')
            }
            else {
                return Promise.reject(res.data.msg)
            }
        }, (error) => {
            return Promise.reject(error)
        }).catch((error) => {
            return Promise.reject(error)
        })
    }
}